package com.huwaiwai.jvm.d20191227;

import java.lang.reflect.Method;

/**
 * @author 歪大哥😁
 * @date 2019-12-27.
 */
public class ReflectMethod {

    public static void target(int i) {
        new Exception("#" + i).printStackTrace();
    }

    public static void main(String[] args) throws Exception {
        Class<?> clazz = Class.forName("com.huwaiwai.jvm.d20191227.ReflectMethod");
        Method method = clazz.getMethod("target", int.class);
        // 测试反射的委派实现和动态实现
        /* 前16次通过本地方法调用实现反射 NativeMethodAccessorImpl.invoke0   栈轨迹为
            at com.huwaiwai.jvm.d20191227.ReflectMethod.target(ReflectMethod.java:12)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:498)
            at com.huwaiwai.jvm.d20191227.ReflectMethod.main(ReflectMethod.java:21)
         */
        /* 后4次通过动态实现调用 GeneratedMethodAccessor1.invoke 栈轨迹为
        	at com.huwaiwai.jvm.d20191227.ReflectMethod.target(ReflectMethod.java:12)
            at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:498)
            at com.huwaiwai.jvm.d20191227.ReflectMethod.main(ReflectMethod.java:21)
         */
        for (int i = 0; i < 20; i++) {
            method.invoke(null, i);
        }
    }
}

